package com.googlecode.flair
{
	import com.googlecode.flair.editors.*;
	
	import flash.utils.Dictionary;
	import flash.utils.describeType;
	import flash.utils.getQualifiedClassName;
	
	import mx.containers.Form;
	import mx.containers.FormItem;
	
	/**
	 * 
	 * @author ccPascu
	 * 
	 */
	public class PropertiesEditorGenerator
	{
		public var flexComponents : XML = null;
		private var propertyEditors : Dictionary = new Dictionary();
		
		public final function createPropertyEditors(flairComponent : IFlairComponent) : Form
		{
			
			var editorForm : Form = null;
			
			var component : Object = flairComponent.component;
			
			var componentClassName : String = getQualifiedClassName(component);
			
			if (propertyEditors[componentClassName] is Form) {
				editorForm = propertyEditors[componentClassName] as Form;
				var editors : Array = editorForm.getChildren();
				for each (var aEditor : IFlairEditor in editors) {
					aEditor.component = flairComponent;
				}
				return editorForm;
			} else {
				editorForm = new Form();
				editorForm.percentHeight = 100;
				editorForm.percentWidth = 100;
				propertyEditors[componentClassName] = editorForm;
			}
			
			var definition : XML = describeType(component);
			var accessors : XMLList = definition.accessor.(@access=='readwrite');
			var properties : Array = [];
			for each (var accessor : XML in accessors) 
			{
				var type: String = accessor.@type;
				var property : String = accessor.@name;
				
				if (properties.indexOf(property) > -1) {
					continue; // Already added.
				}
				properties.push(property);
				var isNotPercentWorH : Boolean= (property.indexOf("Width") > -1 || property.indexOf("Height") > -1) 
					&& (property != "percentWidth" && property != "percentHeight" );
				if ( isNotPercentWorH 
					|| property == "currentState"
					|| property == "uid") {
					continue; // Ignored property.
				}
				
				var editor : IFlairEditor = null;
				
				if (property == "labelPlacement") {
					 
				} else {
					 
				}  
				
				switch(type) 
				{
					case "String":
					//case "Number":
					{
						editor = new TextEditor();
						break;
					}
					case "Number":
					{
						editor = new Slider();
						Slider(editor).minimum = 0;
						if (property == "width" || property == "height" ) {
							Slider(editor).maximum = 3000;
						} else {
							Slider(editor).maximum = 100;
						}
						Slider(editor).step = 1;
						break;
					}
					case "Boolean":
					{
						editor = new BooleanEditor();
						break;
					}
					case "Array":
					{
						editor = new ArrayEditor();
						var componentXml : XML = flexComponents.component.(@name==componentClassName)[0];
						if (componentXml) {
							var prop : XMLList = componentXml.arrayTypes[0].prop.(@name==property);
							if (prop.length() > 0) {
								var arrayType : String = prop[0].@type;
								ArrayEditor(editor).arrayType = arrayType.toString();
							} else {
								editor = null;
							}
						} else {
							editor = null;
						}
						break;
					}
					default :
					{
						editor = null;
					}
				}
				if (editor) {
					var label : String = property.replace(/([A-Z])/, " $1");
					label = label.charAt(0).toUpperCase() + label.substr(1);
					FormItem(editor).label = label;
					editor.component = flairComponent;
					editor.property = property;
					//FormItem(editor).width = 110;
					//FormItem(editor).height = 17;
					editorForm.addChild(FormItem(editor));
				}
			}
			return editorForm;
		}
		
	}
}